package cn.hairui.blog.controller;


import cn.hairui.blog.configure.WebLog;
import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.entity.Message;
import cn.hairui.blog.entity.User;
import cn.hairui.blog.service.MessageService;
import cn.hairui.blog.service.MessagereplyService;
import cn.hairui.blog.util.DateUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 留言表 前端控制器
 * </p>
 *
 * @author laoganbu
 * @since 2020-04-20
 */
@Controller

public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessagereplyService messagereplySerivce;

    @GetMapping(value = "/message/view")
    @WebLog(description = "留言查看")
    public String view(HttpServletRequest request, Model model,int aid){

        // 分页信息 文章信息
        Integer pageNum = null;
        String pageNumStr = request.getParameter("pageIndex");
        if (pageNumStr != null) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        int pageSize = 5;
        PageHelper.startPage(pageNum, pageSize);

        //通过文章编号获取文章下的所有回复 aid=-1表示留言板上的内容

        List<Map> messgaesList = messageService.selectByArticalId(aid);
        PageInfo<Map> pageInfo = new PageInfo<Map>(messgaesList);

        List<Map> messages = new ArrayList<>();
        Iterator iterator = messgaesList.iterator();
        while (iterator.hasNext()) {
            Map map = (Map) iterator.next();
            int msgId = (int) map.get("id");
            //通过msgId获取回复信息
            List<Map> messagereplysList = messagereplySerivce.selectByMsgId(msgId);
            map.put("messagereplys", messagereplysList);
            messages.add(map);
        }

        /*model.addAttribute("messages",messgaesList);*/
        model.addAttribute("messages",messages);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageIndex", pageNum);
        model.addAttribute("pageSize", pageSize);

        model.addAttribute("articalId",aid);
        return "message";
    }


    @RequestMapping("/message/add")
    @ResponseBody
    @WebLog(description = "添加留言")
    public String add(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int articalId = Integer.parseInt(request.getParameter("articalId"));
        String content = request.getParameter("content");

        User user = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if(user == null || user.getAccount() == null){
            map.put(PubConstant.flag, PubConstant.failed);
            return JSONUtils.toJSONString(map);
        }
        Message message = new Message();
        message.setArticalId(articalId);
        message.setContent(content);
        message.setAccount(user.getAccount());
        message.setMessageTime(DateUtil.getCurrentTime());
        message.setStatus(PubConstant.APPROVE_STS_1);

        messageService.save(message);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

}

